using System;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for TriStateControl
/// </summary>
public class TriStateControl : System.Web.UI.UserControl
{
    string cssClass;
    Label lBreak;
    //XmlNode configuration;
    string group;

    RadioButton rbYes = new RadioButton();
    RadioButton rbNo = new RadioButton();
    Label lClearSelection = new Label();

    //protected override void OnInit(EventArgs e)
    //{
    //    //Page.RegisterRequiresControlState(this);
    //    base.OnInit(e);
    //}
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        lClearSelection.Attributes["onclick"] = "clearSelection('" + rbYes.ClientID + "');clearSelection('" + rbNo.ClientID + "');";

        Page.RegisterStartupScript("TriStateControlClearSelection", "<script>function clearSelection(target){var ctl=document.getElementById(target);ctl.checked=false;}</script>");
        ScriptManager.AddShowHideDependents(Page);
    
    }

    public TriStateControl()
    {
    }

    public string GroupName
    {
        get { return group; }
        set { group = value; }
    }


    protected override void CreateChildControls()
    {
        base.CreateChildControls();


        lBreak = new Label();

        if (string.IsNullOrEmpty(group)) group = this.UniqueID;

        // group = configuration.Attributes["id"].Value;
        // read configuration to determine the values below:
        // if not specified, default to Yes/No

        rbYes.ID = group + "Yes";
        rbNo.ID = group + "No";

        rbYes.GroupName = "Group_" + group;
        rbNo.GroupName = "Group_" + group;

        rbYes.Text = "Yes";
        rbNo.Text = "No";
        lBreak.Text = "<br/>";
        lClearSelection.Text = "Clear Selection";
        lClearSelection.CssClass = "login-text";
        lClearSelection.Style.Add("cursor", "pointer");

        rbYes.Attributes["onclick"] = "ShowDependents('" + group + "', 'yes');";
        rbNo.Attributes["onclick"] = "ShowDependents('" + group + "', 'no');";

        rbYes.CssClass = cssClass;
        rbNo.CssClass = cssClass;

        this.Controls.Add(rbYes);
        this.Controls.Add(rbNo);
        this.Controls.Add(lBreak);
        this.Controls.Add(lClearSelection);
    }

    public bool? Value
    {
        get
        {
            this.EnsureChildControls();
            if (rbYes.Checked) return true;
            else if (rbNo.Checked) return false;
            else return null;
        }
        set
        {
            this.EnsureChildControls();
            if (!value.HasValue || value==null)
            {
                rbYes.Checked = false;
                rbNo.Checked = false;
            }
            else if (value.Value)
            {
                rbYes.Checked = true;
                rbNo.Checked = false;
            }
            else if (!value.Value)
            {
                rbYes.Checked = false;
                rbNo.Checked = true;
            }
        }
    }
    public string CssClass
    {
        get
        {
            return cssClass;
        }
        set
        {
            cssClass = value;
        }
    }
}
